package musicstore.action;

import java.sql.Date;
import java.util.List;

import musicstore.biz.AlbumBiz;
import musicstore.biz.OrderBiz;
import musicstore.biz.OrderDetailBiz;
import musicstore.entity.CartItem;
import musicstore.entity.Order;
import musicstore.entity.OrderDetail;
import musicstore.entity.ShoppingCart;
import musicstore.entity.User;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class OrderAction extends ActionSupport {
	
	private Order order;
	public void setOrder(Order order) {
		this.order = order;
	}
	public Order getOrder() {
		return order;
	}

	private OrderBiz orderBiz;
	private OrderDetailBiz orderDetailBiz;
	private AlbumBiz albumBiz;
	public void setOrderBiz(OrderBiz orderBiz) {
		this.orderBiz = orderBiz;
	}
	public void setOrderDetailBiz(OrderDetailBiz orderDetailBiz) {
		this.orderDetailBiz = orderDetailBiz;
	}
	public void setAlbumBiz(AlbumBiz albumBiz) {
		this.albumBiz = albumBiz;
	}
	public String addOrder(){
		ShoppingCart cart = (ShoppingCart) ActionContext.getContext().getSession().get("cart");
		User user = (User) ActionContext.getContext().getSession().get("user");
		if(user==null)
			return LOGIN;
		Date currentDate = new Date(System.currentTimeMillis());
		order.setOrderDate(currentDate);
		order.setIsDeliveried(0);
		order.setTotalPrice(cart.getTotal());
		orderBiz.add(order);
		for(CartItem item : cart.getItems()){
			OrderDetail o = new OrderDetail(0, order,
					albumBiz.getAlbumById(item.getAlbumId()), item.getQuantity(), item.getQuantity());
			orderDetailBiz.addDetails(o);
		}
		ActionContext.getContext().getSession().put("cart",null); 
		return SUCCESS;
	}
	
	private int isDeliveried;
	private Date startDate;
	private Date endDate;
	private List<Order> orderList;
	private int orderId;
	
	public void setOrderId(int orderId) {
		this.orderId = orderId;
	}
	public List<Order> getOrderList() {
		return orderList;
	}
	public void setIsDeliveried(int isDeliveried) {
		this.isDeliveried = isDeliveried;
	}
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	public int getIsDeliveried() {
		return isDeliveried;
	}
	public Date getStartDate() {
		return startDate;
	}
	public Date getEndDate() {
		return endDate;
	}
	public int getOrderId() {
		return orderId;
	}
	
	
	public String orderList(){
		orderList = orderBiz.getListByDate(isDeliveried,orderId, startDate, endDate);
		return SUCCESS;
	}
	
	private int id;
	public void setId(int id) {
		this.id = id;
	}
	
	public String orderDetail(){
		order = orderBiz.getOrderById(id);
		return SUCCESS;
	}
	
}
